package com.fin.zw.intelligentbot.service;

import com.fin.zw.intelligentbot.entity.QVerbalNode;
import com.fin.zw.intelligentbot.entity.QVerbalTrick;
import com.fin.zw.intelligentbot.entity.VerbalNode;
import com.fin.zw.intelligentbot.entity.VerbalTrick;
import com.fin.zw.intelligentbot.model.request.VerbalTrickRequest;
import com.fin.zw.intelligentbot.model.session.SessionModel;
import com.fin.zw.intelligentbot.repository.VerbalNodeRepository;
import com.fin.zw.intelligentbot.repository.VerbalTrickRepository;
import com.google.common.collect.Lists;
import com.querydsl.core.BooleanBuilder;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Iterator;
import java.util.List;
import java.util.Optional;

@Service("verbalTrickService")
public class VerbalTrickService {

    @Autowired
    private VerbalTrickRepository verbalTrickRepository;

    @Autowired
    private VerbalNodeRepository verbalNodeRepository;

    @Autowired
    private CommonService commonService;

    /**
     * 话术信息的查询
     *
     * @param pageable
     * @param request
     * @param sessionModel
     * @return
     */
    public Page<VerbalTrick> queryVerbalTrick(Pageable pageable, VerbalTrickRequest request, SessionModel sessionModel) {
        BooleanBuilder builder = new BooleanBuilder();
        QVerbalTrick qVerbalTrick = QVerbalTrick.verbalTrick;
        if (StringUtils.isNotBlank(sessionModel.getCompanyId())) {
            builder.and(qVerbalTrick.company.id.eq(sessionModel.getCompanyId()));
        }
        if (StringUtils.isNotBlank(request.getName())) {
            builder.and(qVerbalTrick.name.eq(request.getName()));
        }
        Page<VerbalTrick> page = verbalTrickRepository.findAll(builder, pageable);
        return page;
    }


    /**
     * 话术的添加
     *
     * @param request
     * @param sessionModel
     */
    public void createVerbalTrick(VerbalTrickRequest request, SessionModel sessionModel) {
        VerbalTrick verbalTrick = new VerbalTrick();
        BeanUtils.copyProperties(request, verbalTrick);
        verbalTrick.setCompany(commonService.getCompany(sessionModel));
        verbalTrickRepository.save(verbalTrick);
    }

    /**
     * 话术的修改
     *
     * @param request
     */
    public void updateVerbalTrick(VerbalTrickRequest request) {
        Optional<VerbalTrick> optional = verbalTrickRepository.findById(request.getId());
        if (!optional.isPresent()) {
            throw new RuntimeException("修改的话术信息不存在");
        }
        VerbalTrick verbalTrick = optional.get();
        BeanUtils.copyProperties(request, verbalTrick);
        verbalTrickRepository.save(verbalTrick);
    }

    /**
     * 话术的删除
     *
     * @param id
     */
    @Transactional
    public void deleteVerbalTrick(String id) {
        Iterator<VerbalNode> iterator = verbalNodeRepository.findAll(QVerbalNode.verbalNode.verbalId.eq(id)).iterator();
        List<VerbalNode> list = Lists.newArrayList(iterator);
        verbalNodeRepository.deleteAll(list);
        verbalTrickRepository.deleteById(id);
    }
}
